package view.rules;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import features.Feature;
import features.FeatureList;

import view.grammardevelopment.MainFrame;

public class FeaturePane extends JPanel{
	
	public JPanel panel;
    public JTextArea textArea;
	private JScrollPane scrollPane;

	private Map<String,ArrayList<String>> featureList;

	private Map<String,ArrayList<String>> selectedFeatures;

	public FeaturePane(){
		this.setLayout(new MigLayout());
		
		initComponent();
		addComponent();
	}
	
	private void initComponent(){
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		panel = new JPanel();
		panel.setLayout(new MigLayout());
		
		textArea = new JTextArea();

        textArea.setMinimumSize(new Dimension((int)screenSize.getWidth() - 200, (int)screenSize.getHeight() - 600));
		
		panel.add(textArea);
	
		scrollPane = new JScrollPane(panel);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setPreferredSize(new Dimension(MainFrame.getInstance().getWidth() - 500, 200));	
		
		selectedFeatures = new HashMap<String, ArrayList<String>>();

	}

	private void addComponent(){
		add(scrollPane);
	}
	
	public FeatureList getFeatureList(){
		ArrayList<Feature> features = new ArrayList<Feature>();
		Set<String> keys = featureList.keySet();

		for(String k: keys){
			ArrayList<String> list = featureList.get(k);
			for(String f: list){
				Feature feat = new Feature(k,f,true);
				features.add(feat);
			}
		}
		return new FeatureList(features);
	}
	public void setTextArea( Map<String,ArrayList<String>>  selectedValues){

		this.featureList = selectedValues;

		this.selectedFeatures = selectedValues;

		String values = "";

	    Iterator it = selectedFeatures.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        values = values + pairs.getKey() + " = " + pairs.getValue() + " \n " ;
	        //it.remove(); // avoids a ConcurrentModificationException
	    }
	    
		textArea.setText(values);
	}
	
	public Map<String, ArrayList<String>> getFeatures(){
		return this.selectedFeatures;
	}
}
